<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/14
 * Time: 13:03
 */
namespace app\admin\controller;

use app\admin\validate\Message as validateMessage;
use app\admin\model\Message as modelMessage;
use think\facade\Hook;

class Message extends Base{
    public function __construct(){
        parent::__construct();
    }

    /**
     * @param array $where
     * @param string $order
     * @param int $sql
     * @return array
     * @throws \think\exception\DbException
     */
    public function index($where=[],$order='-create_time'){
        $where = $this->_where($where);
        $order = $this->_order($order);
        $column = new modelMessage;
        $_column =$column::where($where)->order($order)->paginate();
        $this->__s('',$_column);
    }

    /**
     * @param int $id
     * @param int $limit
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save($id=0,$limit=1000){
        $data = $this->request->post();
        $valid = new validateMessage();
        if(!$valid->check($data)){
            return $this->__e($valid->getError());
        }
        $model = new modelMessage();
        if(!$model->save($data)){
            return $this->__e('操作失败');
        }
        $sql = $model->getLastSql();
        $params = [
            'aid'=>self::$uid,
            'action'=>$id?'更新':'添加'."消息推送，编号：{$id}",
            'sql'=>$sql
        ];
        $payload = [
            'title'=>$data['title'],
            'content'=>$data['content'],
            'payload'=>[
                'type'=>4,
                'id'=>$data['rid'],
                'cid'=>$data['cid']
            ]
        ];
        $push = new Push();
        // 接收类型:1所有,2月会员,3年会员,4指定推送,5认证用户,6非认证用户
        if($data['res_type']==1){
            $res = $push->pushAll($payload);
        }else{
            $model = new \app\admin\model\Member();
            if($data['res_type']==2){
                $count = $model
                    ->where('vip_type','eq',1)
                    ->count(1);
                $page = ceil($count/$limit);
                for ($i=1;$i<=$page;$i++){
                    $list = $model
                        ->field('id,client_id')
                        ->where('vip_type','eq',1)
                        ->where('client_id','neq','')
                        ->page($i,$limit)
                        ->select();
                    $arr =[];
                    foreach ($list as $v){
                        if($v['client_id']){
                            array_push($arr,$v['client_id']);
                        }
                    }
                    $push->pushList($arr,$payload);
                }
            }else if($data['res_type']==3){
                $count = $model
                    ->where('vip_type','eq',1)
                    ->count(1);
                $page = ceil($count/$limit);
                for ($i=1;$i<=$page;$i++){
                    $list = $model
                        ->field('id,client_id')
                        ->where('vip_type','eq',2)
                        ->where('client_id','neq','')
                        ->page($i,$limit)
                        ->select();
                    $arr =[];
                    foreach ($list as $v){
                        if($v['client_id']){
                            array_push($arr,$v['client_id']);
                        }
                    }
                    $push->pushList($arr,$payload);
                }
            }else if($data['res_type']==4){
                $send_user = explode("\n",$data['send_user']);
            }else{
                $count = $model
                    ->where('vip_type','eq',1)
                    ->count(1);
                $page = ceil($count/$limit);
                for ($i=1;$i<=$page;$i++){
                    $list = $model
                        ->field('id,client_id')
                        ->whereIn('id',$data['mid'])
                        ->where('client_id','neq','')
                        ->page($i,$limit)
                        ->select();
                    $arr =[];
                    foreach ($list as $v){
                        if($v['client_id']){
                            array_push($arr,$v['client_id']);
                        }
                    }
                    $push->pushList($arr,$payload);
                }
            }
        }
        Hook::listen('admin_log',$params);
        return $this->__s('操作成功', $res);
    }

    public function delete($id=0){
        if(!$id){
           $this->__e('缺少id');
        }
        $id = str_replace('_',',',$id);
        $column = new modelMessage;
        if(!$column::destroy($id)){
            $this->__e('删除失败');
        }
        $sql = $column->getLastSql();
        $params = [
            'aid'=>self::$uid,
            'action'=>$id?'更新':'添加'."消息推送，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$params);
        return $this->__s('删除成功');
    }


    public function edit($id=0){
        if(!$id){
            $this->__e('缺少id');
        }
        $column = new modelMessage;
        $_column = $column::get($id);
        if(!$_column){
            $this->__e('没有数据');
        }
        return $this->__s('',$_column);
    }

    public function read($id=0){
        if(!$id){
            $this->__e('缺少id');
        }
        $column = new modelMessage;
        $_column = $column::get($id);
        if(!$_column){
            $this->__e('没有数据');
        }
        $this->__s('',$_column);
    }

    public function update($id=0,$status=-1){
        if(!$id){
            $this->__e('缺少id');
        }
        $column = new modelMessage;
        $_column = $column::get($id);
        if(!$_column){
            $this->__e('没有数据');
        }
        $_column->status=$status;
        if(!$_column->save()){
            $this->__e('修改失败');
        }
        $this->__s('修改成功');
    }
}